<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ModuleManageCreateCommand extends Command
{
    /**
     * command name
     */
    protected $signature = 'ModuleManage:create {moduleName}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Module manager create module.';

    /**
     * Handle Function!
     */
    public function handle()
    {
        $moduleName = $this->argument('moduleName');
        $ControllerPath = base_path('app/Http/Controllers/'.$moduleName);
        $MiddlewarePath = base_path('app/Http/Middleware/'.$moduleName);
        $ValidatorsPath = base_path('app/Http/Validators/'.$moduleName);
        $ConsolePath = base_path('app/Console/Commands/'.$moduleName);
        $ModelsPath = base_path('app/Models/'.$moduleName);

        if (is_dir($ControllerPath)) {
            throw new \Exception("This module already exists. Please do not create it again!");
        }

        try {
            mkdir($ControllerPath, 0666,true);
            mkdir($MiddlewarePath, 0666,true);
            mkdir($ValidatorsPath, 0666,true);
            mkdir($ConsolePath, 0666,true);
            mkdir($ModelsPath, 0666,true);

            @chmod($ControllerPath, 0666);
            @chmod($MiddlewarePath, 0666);
            @chmod($ValidatorsPath, 0666);
            @chmod($ConsolePath, 0666);
            @chmod($ModelsPath, 0666);
            $this->createController($moduleName, $ControllerPath);
            $this->createRoute($moduleName);
            echo "Create module {$moduleName} success!" . PHP_EOL . "Please use http://host/{$moduleName}/index visit!";
        } catch (\Throwable $exception) {
            is_dir($ControllerPath) && @rmdir($ControllerPath);
            is_dir($MiddlewarePath) && @rmdir($MiddlewarePath);
            is_dir($ValidatorsPath) && @rmdir($ValidatorsPath);
            is_dir($ConsolePath) && @rmdir($ConsolePath);
            is_dir($ModelsPath) && @rmdir($ModelsPath);
            throw new \Exception($exception->getMessage());
        }
    }

    public function createController($moduleName, $basePath) {
       $controllerContent=<<<EOL
<?php
namespace App\Http\Controllers\\$moduleName;

use Illuminate\Http\Request;
class ExampleController{
  public function index(Request \$request)
  {
    return ApiReturn(RETURN_SUCCESS, "Start your project {$moduleName}!");
  }
}
EOL;
       $filePath = $basePath . '/ExampleController.php';
       $f = fopen($filePath, 'w+');
       fwrite($f, $controllerContent);
       fclose($f);
    }

    public function createRoute($moduleName) {
        $routePath = base_path("routes/{$moduleName}.php");
        $routeContent =<<<EOL
<?php
\$router->get('/{$moduleName}/index', ['uses' => '{$moduleName}\ExampleController@index']);
EOL;
        $f = fopen($routePath, 'w+');
        fwrite($f, $routeContent);
        fclose($f);

        $str = "\nrequire_once __DIR__ . '/{$moduleName}.php';";
        $baseRootPath = base_path("routes/web.php");
        $f = fopen($baseRootPath, 'a+');
        fwrite($f, $str);
        fclose($f);
    }
}
